package com.usernet.product.utils;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.usernet.product.dao.AdminLogDao;
import com.usernet.product.entity.AdminLog;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

public class ProductUtils {
	/**
	 * 获取IP地址
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public static void main(String[] args) {
		System.out.println(getMD5String("guest"));
	}
	/*
	 * 加密
	 */
	public static String getMD5String(String args) {
		return Base64.encode(Md5Token.getInstance().getLongToken(
				Md5Token.getInstance().getLongToken(args)
						+ ProductConfig.MD5_CODE), ProductConfig.CHARSET);
	}

	/**
	 * 获得汉字的首字母拼音
	 * 
	 * @param chinese
	 *            汉字
	 * @return
	 */
	public static String getFirstChars(String chinese) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		try {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < chinese.length(); i++) {
				// 对英文字母的处理：小写字母转换为大写，大写的直接返回
				char ch = chinese.charAt(i);
				if (ch >= 'a' && ch <= 'z')
					sb.append(ch);
				if (ch >= 'A' && ch <= 'Z')
					sb.append(ch);
				if (ch <= '9' && ch >= '0')
					sb.append(ch);
				// 对汉语的处理
				String[] array = PinyinHelper.toHanyuPinyinStringArray(chinese
						.charAt(i), format);
				if (array == null || array.length == 0) {
					continue;
				}
				String s = array[0];
				sb.append(s.charAt(0));
			}
			return sb.toString();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获得汉字的全拼
	 * 
	 * @param chinese
	 *            汉字
	 * @return
	 */
	public static String getFullChars(String chinese) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		try {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < chinese.length(); i++) {
				// 对英文字母的处理：小写字母转换为大写，大写的直接返回
				char ch = chinese.charAt(i);
				if (ch >= 'a' && ch <= 'z')
					sb.append(ch);
				if (ch >= 'A' && ch <= 'Z')
					sb.append(ch);
				if (ch <= '9' && ch >= '0')
					sb.append(ch);
				// 对汉语的处理

				String[] array = PinyinHelper.toHanyuPinyinStringArray(ch,
						format);
				if (array == null || array.length == 0) {
					continue;
				}
				String s = array[0];
				sb.append(s);
			}
			return sb.toString();
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 添加管理员操作记录
	 * 
	 * @param request
	 * @param username
	 *            登录名
	 * @param actions
	 *            操作说明
	 * @param remark
	 *            备注
	 */
	public static void addAdminLog(HttpServletRequest request, String username,
			String actions, String remark) {
		AdminLog log = new AdminLog();
		log.setUsername(username);
		log.setRemark(remark);
		log.setActions(actions);
		log.setTime(new Timestamp(System.currentTimeMillis()));
		log.setIp(getIpAddr(request));
		new AdminLogDao().save(log);
	}

}
